package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import model.ConferencePaper;

/**
 * This class is panel that is responsible for displaying the
 * options for submitting a recommendation for a conference paper.
 * 
 * @author Darrion Harris
 *
 */
@SuppressWarnings("serial")
public class SPCRecommendationPanel extends Container implements ActionListener{

	ConferencePaper my_paper;
	JTextArea my_file_path_display;
	JButton my_browseb_button;
	JButton my_submit_button;
	JFileChooser my_chooser = new JFileChooser();

	public SPCRecommendationPanel(ConferencePaper the_paper){
		my_paper = the_paper;
    	this.setLayout(new BorderLayout());
    	
		this.add(createUploadPanel(), BorderLayout.CENTER);
		this.setBackground(Color.white);
        
	}
	
	/**
	 * Creates the panel for uploading a recommendation
	 * @return the panel with the uploading function for 
	 * uploading recommendations.
	 */
	private JPanel createUploadPanel() {
		JPanel p = new JPanel(new FlowLayout());
		my_file_path_display = new JTextArea("BROWSE FOR FILE TO SUBMIT");
		my_file_path_display.setEditable(false);
		my_file_path_display.setPreferredSize(new Dimension((SPCViewport.WIDTH - 50)/3, 30));
		my_browseb_button = new JButton("BROWSE");
		my_browseb_button.addActionListener(this);
		my_submit_button = new JButton("SUBMIT");
		my_submit_button.addActionListener(this);
		my_submit_button.setEnabled(false);
		p.add(my_file_path_display);
		p.add(Box.createRigidArea(new Dimension(30,30)));
		p.add(my_browseb_button);
		p.add(my_submit_button);
		p.setOpaque(false);
		return p;
	}
	
	/**
	 * Code recycled from Tyler
	 * This method copies the file into the file directory by using the FileInputStream
	 * and the FileOutputStream <Java API is quite helpful here>.
	 * 
	 * @param the_input_file to copy
	 * @return the file that was copied
	 */
	private File copyFile(File the_input_file)
	{
		File newFile = null;
		FileInputStream input;
		try 
		{
			input = new FileInputStream(the_input_file);
			newFile = new File("Recommendations" , the_input_file.getName());
			FileOutputStream output = new FileOutputStream(newFile);
			byte[] stream = new byte[input.available()];
			input.read(stream);
			output.write(stream);
			output.close();
			input.close();
		}
		catch (FileNotFoundException e) 
		{
			System.err.println("File not found. Whoops!");
		} 
		
		catch (IOException e) 
		{
			System.err.println("IO Exception");
		}
		
		return newFile;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == my_browseb_button){
			int userChoice = my_chooser.showOpenDialog(null);
			if(userChoice == JFileChooser.APPROVE_OPTION)
			{
				File file = my_chooser.getSelectedFile();
				my_file_path_display.setText(file.getAbsolutePath());
				my_submit_button.setEnabled(true);
			}
		}
		
		else if(e.getSource() == my_submit_button) {
			File recommendation = new File(my_file_path_display.getText());
			my_paper.setRecommendation(my_file_path_display.getText());
			
			if(recommendation.exists())
			{
				copyFile(recommendation);
				SPCViewport.loadPapersPanel();
			}
			
		}
		
	}
}
